<?php


namespace app\zhongcan\controller;


use app\zhongcan\model\NewsModel;
use app\zhongcan\validate\NewsValidate;
use cmf\controller\AdminBaseController;

class NewsController extends AdminBaseController
{
    public function index(){
        $data = NewsModel::select();
        $key=input('key','');
        $where=[];
        if($key){
            $where[]=['title|author','like',"%$key%"];
        }
        $list=NewsModel::order('list_order','desc')
            ->where($where)
            ->paginate(4);
        $this->assign('list',$list);
        return $this->fetch();
    }
    public function add(){
        return  $this->fetch();
    }
    public function addPost(){
        //接收数据
        $data = input('post.');
        //验证器
        $validate = new NewsValidate();
        $res = $validate-> scene('add')->check($data);
            if(!$res){
            $this->error($validate->getError());
            }
        $data['create_time'] = date('Y-m-d H:i:s');
        //入库
        if(NewsModel::create($data)){
            $this->success('添加成功','News/index');
        }else{
            $this->error('添加失败');
        }

    }
    public function edit()
    {
        $id = input('id');
        $new = NewsModel::where('id', '=', $id)
            ->find();
        $content1 = htmlspecialchars_decode($new['content']);
        $content2 = cmf_replace_content_file_url($content1);
        $new['content'] = $content2;
        $this->assign('news', $new);
        return $this->fetch();
    }
    public function editPost(){
        //接收
        $data=input('post.');
        $content1=htmlspecialchars_decode($data['content']);
        $content2=cmf_replace_content_file_url($content1,true);
        $data['content']=$content2;
        $v=new NewsValidate();
        $res=$v->scene('edit')->check($data);
        if(!$res){
            $this->error($v->getError());
        }
        $model=NewsModel::find($data['id']);

        $model->title=$data['title'];
        $model->image=$data['image'];
        $model->look_num=$data['look_num'];
        $model->author=$data['author'];
        $model->abstract=$data['abstract'];
        $model->content=$data['content'];
        if($model->save()){
            $this->success('修改成功','News/index');
        }else{
            $this->error('修改失败');
        }
    }
    public function del(){
        $id=input('id/d');
        $model=NewsModel::destroy($id);
        if($model){
            $this->success('删除成功','News/index');
        }else{
            $this->error('删除失败');
        }
    }
    //排序
    public function px(){
        $model=new NewsModel();
        parent::listOrders($model);
        $this->success('排序成功');
    }
    //预览文章
    public function detail(){
        $id =input('id');
        $News=NewsModel::find($id);
        $content1=htmlspecialchars_decode($News['content']);
        $content2=cmf_replace_content_file_url($content1,true);
        $data['content']=$content2;
        $this->assign('news',$News);
        return $this->fetch('detail');
    }
    //显示状态的切换
    public function changeStatus(){
        $id=input('id/d');
        $News=NewsModel::find($id);
        if($News['switch']==0){
            $Newsstatus = 1;
        }else{
            $Newsstatus = 0;
        }
        $News->switch = $Newsstatus;
        if($News->save()){
            $this->success('修改成功');
        }
    }
}